/**
 * 
 */
package xx.services.entity.rest;

import java.sql.Date;

import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.DELETE;
import javax.ws.rs.FormParam;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.Context;

import xx.entity.bas.ProjectPay;
import xx.services.Passport;
import xx.services.entity.inter.IPayInterface;
import xx.services.util.Constants;

/**
 * 实体支付明细服务接口<br>
 * 
 * @Path (Constants.RESTFUL_ROOT_PATH + "/pay")
 * @author dengjinye,2013-5-18 $LastChangedDate$ $Author$
 */
@Path(Constants.RESTFUL_ROOT_PATH + "/pay")
public class PayRest extends Passport implements IPayInterface
{

    /**
     * @param request
     */
    public PayRest(@Context
    HttpServletRequest request)
    {
        super(request);
    }

    /**
     * 更新实体的支付明细
     * 
     * @PUT
     * @Path ("/{pay_id}")
     * @param payId
     *            支付明细ID
     * @param paydate
     *            支付日期
     * @param payprice
     *            支付金额
     * @param ord
     *            第N次支付
     * @return
     * @author dengjinye,2013-5-29
     */
    @PUT
    @Path("/{pay_id}")
    public String updatePayList(@PathParam("pay_id")
    long payId, @FormParam("paydate")
    String paydate, @FormParam("payprice")
    String payprice, @FormParam("ord")
    String ord)
    {
        try
        {
            ProjectPay projectPay = new ProjectPay(qr, owner);
            projectPay.setPayId(payId);

            StringBuffer updateColumn = new StringBuffer();
            if (paydate != null && !paydate.isEmpty())
            {
                projectPay.setPaydate(Date.valueOf(paydate));
                updateColumn.append(ProjectPay.PAYDATE).append(",");
            }
            if (payprice != null && !payprice.isEmpty())
            {
                projectPay.setPayprice(Double.valueOf(payprice));
                updateColumn.append(ProjectPay.PAYPRICE).append(",");
            }
            if (ord != null && !ord.isEmpty())
            {
                projectPay.setOrd(Integer.valueOf(ord));
                updateColumn.append(ProjectPay.ORD).append(",");
            }

            String uc = updateColumn.toString();
            if (uc != null && !uc.isEmpty())
            {
                projectPay.update(uc.split(","));
            }

            addUrlLog(String.valueOf(projectPay.getPayId()),String.valueOf(projectPay.getEntityId()));
            return ok(projectPay);
        }
        catch (Exception e)
        {
            return error(e);
        }
    }

    /**
     * 删除实体的支付明细
     * 
     * @DELETE
     * @Path ("/{pay_id}")
     * @param payId
     *            -@PathParam("pay_id") 支付明细id，多个id使用“_”间隔
     * @return 删除条数
     * @author dengjinye,2013-5-29
     */
    @DELETE
    @Path("/{pay_id}")
    public String deletePayList(@PathParam("pay_id")
    String payId)
    {
        try
        {
            int delCount = 0;
            String[] payIdArr = payId.split("_");
            for (String id : payIdArr)
            {
                ProjectPay projectPay = new ProjectPay(qr, owner);
                projectPay.setPayId(Long.valueOf(id)).delete();
                delCount++;
                addUrlLog(String.valueOf(id),String.valueOf(projectPay.getEntityId()));
            }
            return ok(delCount);
        }
        catch (Exception e)
        {
            return error(e);
        }
    }

    /**
     * 获取指定paiId的支付详细信息
     * 
     * @Path ("/{pay_id}")
     * @param payId
     * @return payId信息
     * @author dengjinye,2013-5-30
     */
    @GET
    @Path("/{pay_id}")
    public String getPay(@PathParam("pay_id")
    long payId)
    {
        try
        {
            ProjectPay projectPay = new ProjectPay(qr, owner);
            projectPay.setPayId(payId).load();
            return ok(projectPay);
        }
        catch (Exception e)
        {
            return error(e);
        }
    }

}
